# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy
import re
import datetime
from scrapy.loader import ItemLoader
from scrapy.loader.processors import MapCompose, TakeFirst, Join


class ArticlespiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass


def date_convert(value):
    if u"\xb7" in value:
        value = value.strip().replace(u"\xb7", "")
    try:
        create_time = datetime.datetime.strptime(value, "%Y%m%d").date()
    except Exception as e:
        create_time = datetime.datetime.now().date()
    return create_time


def get_nums(value):
    match_re = re.match(".*(\d+).*", value)
    if match_re:
        nums = int(match_re.group(1))
    else:
        nums = 0
    return nums


def remove_comments_tags(value):
    # 去除tag中的评论
    if "评论" in value:
        return ""
    else:
        return value


def return_value(value):
    return value


class ArticleItemLoader(ItemLoader):
        #    自定义itemloader
    default_output_processor = TakeFirst()


class JobBoLeArticleItem(scrapy.Item):
    # Field类型
    title = scrapy.Field()
    create_time = scrapy.Field(
        input_processor = MapCompose(date_convert),
    )
    url = scrapy.Field()
    # url的md5
    url_object_id = scrapy.Field()
    front_image_url = scrapy.Field(
        input_processor=MapCompose(return_value)
    )
    front_image_path = scrapy.Field()
    praise_nums = scrapy.Field(
        input_processor=MapCompose(get_nums),
    )
    fav_nums = scrapy.Field(
        input_processor=MapCompose(get_nums),
    )
    comment_nums = scrapy.Field(
        input_processor=MapCompose(get_nums),
    )
    tags = scrapy.Field(
        input_processor = MapCompose(remove_comments_tags),
        output_processor=Join(","),
    )
    content = scrapy.Field()
